// const baseUrl = 'https://lsq.roxars.com/heritage_api/wechat'
import { sha256 } from "./sha256";
import uuidv4 from "./uuidv4";
const app = getApp();
// const baseUrl = "https://app.9linked.net/isldcapi";
// const baseUrl = "https://test2.9linked.net/isldcapi";
// const baseUrl = "http://192.168.1.101:8081/api";
// const baseUrl = "http://192.168.1.122:8083/fytestApp";
// const baseUrl = "http://4z8172f519.qicp.vip";
const baseUrl = "https://test2.9linked.net/fytestApp";
const http = ({ url = "", param = {}, headers = {}, ...other } = {}) => {
    return new Promise((resolve, reject) => {
        // console.log(12345);
        // let sessionId = dd.getStorageSync("sessionId") ?
        //     dd.getStorageSync("sessionId") :
        //     "";
        // const request_id = uuidv4();
        // const timestamp = new Date().getTime();
        // let signature = sha256(
        //     `${sessionId}&&${request_id}&&${timestamp}&&E&2!36Z&4*M#`
        // );
        const header = {
            "content-type": "application/x-www-form-urlencoded",
            //"content-type": "application/json",
            // "X-SESSION-ID": sessionId,
            // "X-REQUEST-ID": request_id,
            // "X-TIMESTAMP": timestamp,
            // "X-SIGNATURE": signature,
            ...headers,
        };
        // if (!other.method) {
        //     dd.showLoading({
        //         title: '加载中'
        //     })
        // }
        dd.httpRequest({
            url: getUrl(url),
            data: {...param },
            headers: header,
            ...other,
            // success: (res) => {

            //     // dd.hideLoading();
            //     // if (res.data.code === 0) {
            //     //     resolve(res.data);
            //     // } else if (res.data.code === 50101) {
            //     //     dd.redirectTo({
            //     //         url: "/pages/login/login",
            //     //     });
            //     // } else {
            //     //     dd.showToast({
            //     //         title: res.data.message,
            //     //         icon: "none",
            //     //     });
            //     // }
            // },
            // fail: (res) => {},
            success: function(res) {
                resolve(res.data);
            },
            fail: function(res) {
                reject(res);
            },
        });
    });
};

const http1 = ({ url = "", param = {}, headers = {}, ...other } = {}) => {
    return new Promise((resolve, reject) => {
        // let sessionId = dd.getStorageSync("sessionId") ?
        //     dd.getStorageSync("sessionId") :
        //     "";
        // const request_id = uuidv4();
        // const timestamp = new Date().getTime();
        // let signature = sha256(
        //     `${sessionId}&&${request_id}&&${timestamp}&&E&2!36Z&4*M#`
        // );
        const header = {
            // "content-type": "application/x-www-form-urlencoded",
            "content-type": "application/json",
            // "X-SESSION-ID": sessionId,
            // "X-REQUEST-ID": request_id,
            // "X-TIMESTAMP": timestamp,
            // "X-SIGNATURE": signature,
            // ...headers,
        };
        // if (!other.method) {
        //     dd.showLoading({
        //         title: '加载中'
        //     })
        // }

        console.log(header)
        dd.httpRequest({
            url: getUrl(url),
            data: param,
            headers: header,
            ...other,
            success: function(res) {
                resolve(res.data);
            },
            fail: function(res) {
                reject(res);
            },
        });
    });
};

const httpimg = ({
    url = "",
    fileType = "",
    fileName = "",
    filePath = {},
    headers = {},
} = {}) => {
    return new Promise((resolve, reject) => {
        // let sessionId = dd.getStorageSync("sessionId") ?
        //     dd.getStorageSync("sessionId") :
        //     "";
        // const request_id = uuidv4();
        // const timestamp = new Date().getTime();
        // let signature = sha256(
        //     `${sessionId}&&${request_id}&&${timestamp}&&E&2!36Z&4*M#`
        // );
        // const header = {
        //     "content-type": "application/x-www-form-urlencoded",
        //     // "content-type": "application/json",
        //     "X-SESSION-ID": sessionId,
        //     "X-REQUEST-ID": request_id,
        //     "X-TIMESTAMP": timestamp,
        //     "X-SIGNATURE": signature,
        //     ...headers,
        // };
        // if (!other.method) {
        //     dd.showLoading({
        //         title: '加载中'
        //     })
        // }
        dd.uploadFile({
            url: getUrl(url),
            fileType: fileType,
            fileName: fileName,
            filePath: filePath,

            // formData: formData,
            // header,
            success: (res) => {


                // dd.hideLoading();
                res = JSON.parse(res.data);
                resolve(res)
                    // if (res.code === 0) {
                    //     console.log(1111);
                    //     resolve(res);
                    // } else if (res.code === 50101) {
                    //     dd.redirectTo({
                    //         url: "/pages/login/login",
                    //     });
                    // } else {
                    //     // dd.showToast({
                    //     //     title: res.data.message,
                    //     //     icon: "none",
                    //     // });
                    // }
            },
            fail: (err) => {
                console.log(err);
            },
        }).catch((e) => {});
    });
};

const getUrl = (url) => {
    if (url.indexOf("://") == -1) {
        url = baseUrl + url;
    }
    return url;
};

// get方法
const _get = (url, param = {}) => {
    return http({
        url,
        param,
    });
};

const _post = (url, param = {}) => {
    return http({
        url,
        param,
        method: "post",
    });
};
const _post1 = (url, param = {}) => {
    return http1({
        url,
        param,
        method: "post",
    });
};
const _put = (url, param = {}) => {
    return http({
        url,
        param,
        method: "put",
    });
};

const _delete = (url, param = {}) => {
    return http({
        url,
        param,
        method: "delete",
    });
};

const loadimg = (url, fileType, fileName, filePath) => {
    return httpimg({
        url,
        fileType,
        fileName,
        filePath,
        method: "post",
    });
};
module.exports = {
    baseUrl,
    _get,
    _post,
    _put,
    _delete,
    _post1,
    loadimg,
};